<?php

namespace App\Http\Controllers\Api\V1;

use App\Models\Order;
use App\Models\Recharge;
use App\Models\GoldCharge;
use App\Models\GoldRecord;
use App\Models\VipPurchase;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\Cache;
use App\Http\Controllers\Api\V1\Resources\OrderResource;
use App\Http\Controllers\Api\V1\Resources\GoldRecordResource;
use App\Http\Controllers\Api\V1\Resources\VipPurchaseResource;

class PaysController extends Controller
{
	public function payStormByGoldCoin(GoldRecord $record): GoldRecordResource
	{
		$lock = Cache::lock('pay_storm_by_gold_coin', 15);

		try {

			if (!$lock->get()) fail('高峰拥挤，请再试');

			$this->authorize('own', $record);
			cashier('goldcoin')->payStorm($record);

            return new GoldRecordResource($record);

		} finally {
			$lock->release();
		}
	}

	public function payOrderByGoldCoin(Order $order): OrderResource
	{
		$lock = Cache::lock('pay_order_by_gold_coin', 15);

		try {

			if (!$lock->get()) fail('高峰拥挤，请再试');

			$this->authorize('own', $order);
			$result = cashier('goldcoin')->payOrder($order);

            return new OrderResource($result);

		} finally {
			$lock->release();
		}
	}


	public function payOrderByGoldBean(Order $order): OrderResource
	{
		$lock = Cache::lock('pay_order_by_gold_coin', 15);

		try {

			if (!$lock->get()) fail('高峰拥挤，请再试');

			$this->authorize('own', $order);
			$result = cashier('goldbean')->payOrder($order);

            return new OrderResource($result);

		} finally {
			$lock->release();
		}
	}


	public function payOrderByApple(Order $order): OrderResource
	{
		$lock = Cache::lock('pay_order_by_apple', 15);

		try {

			if (!$lock->get()) fail('高峰拥挤，请再试');

			$this->authorize('own', $order);
			$user = request()->user();

			if($user->is_stop){
				fail('用户涉嫌刷单，已停止使用小苹果');
			}elseif($user->isDetect()){
				fail('用户涉嫌刷单，暂不可使用小苹果购买商品');
			}
			$result = cashier('apple')->payOrder($order);

            return new OrderResource($result);

		} finally {
			$lock->release();
		}
	}

    public function payOrderByAlipay(Order $order): JsonResponse
    {
	    $lock = Cache::lock('pay_order_by_alipay', 15);

	    $alipay = cs("storm")->is_alipay_open;
		if(empty($alipay)){

			fail('支付宝支付暂不可用');
		}
	    try {

	    	if (!$lock->get()) fail('高峰拥挤，请再试');

		    $this->authorize('own', $order);
		    $result = cashier('alipay')->payOrder($order);

            return response()->json(['result' => $result, 'order_id' => $order->id]);

	    } finally {
	    	$lock->release();
	    }
    }

    public function payOrderByAliwap(Order $order): JsonResponse
    {
    	$wxpay = cs("storm")->is_wxpay_open;
		if(empty($wxpay)){

			fail('微信支付暂不可用');
		}
	    $this->authorize('own', $order);
	    $result = cashier('alipay')->payOrder($order);
    	return response()->json(['result' => $result, 'order_id' => $order->id]);
    }

	public function payGoldChargeByAlipay(GoldCharge $charge): JsonResponse
	{
		fail('请联系您的专属小二线下支付');
		$this->authorize('own', $charge);
		$result = cashier('alipay')->payGoldCharge($charge);
		return response()->json(['result' => $result, 'gold_charge_id' => $charge->id]);
	}

	public function payGoldChargeByWxpay(GoldCharge $charge): JsonResponse
	{
		fail('请联系您的专属小二线下支付');
		$this->authorize('own', $charge);
		$result = cashier('wxpay')->payGoldCharge($charge);
		return response()->json(['result' => $result, 'gold_charge_id' => $charge->id]);
	}

	public function payGoldChargeByAliwap(GoldCharge $charge): JsonResponse
	{
		$this->authorize('own', $charge);
		$result = cashier('alipay')->payGoldCharge($charge);
		return response()->json(['result' => $result, 'gold_charge_id' => $charge->id]);
	}

	public function payOrderByWxpay(Order $order): JsonResponse
    {
        $this->authorize('own', $order);
        $result = cashier('wxpay')->payOrder($order);
        return response()->json(['result' => $result, 'order_id' => $order->id]);
    }

    public function payOrderByAdaWxmini(Order $order): JsonResponse
    {
        $this->authorize('own', $order);
        $result = cashier('adawxmini')->payOrder($order);
        return response()->json(['result' => $result, 'order_id' => $order->id]);
    }

    public function payGoldChargeByAdaWxmini(GoldCharge $charge): JsonResponse
    {
        $this->authorize('own', $charge);
        $result = cashier('adawxmini')->payGoldCharge($charge);
        return response()->json(['result' => $result, 'gold_charge_id' => $charge->id]);
    }

	public function payGoldChargeBySandwap(GoldCharge $charge): JsonResponse
	{
		$this->authorize('own', $charge);
		$result = cashier('sandwap')->payGoldCharge($charge);
		return response()->json(['result' => $result, 'gold_charge_id' => $charge->id]);
	}

	public function payGoldChargeBySandwx(GoldCharge $charge): JsonResponse
	{
		$this->authorize('own', $charge);
		$result = cashier('sandwap')->payGoldCharge($charge);
		return response()->json(['result' => $result, 'gold_charge_id' => $charge->id]);
	}


	public function payGoldChargeByGoldCoin(GoldCharge $charge): JsonResponse
	{
		$this->authorize('own', $charge);
		$result = cashier('goldcoin')->payGoldCharge($charge);
		return response()->json(['result' => $result, 'gold_charge_id' => $charge->id]);
	}


	public function payVipPurchaseByAlipay(VipPurchase $purchase)
    {
    	$alipay = cs("storm")->is_alipay_open;
		if(empty($alipay)){

			fail('支付宝支付暂不可用');
		}
	    $this->authorize('own', $purchase);
	    $result = cashier('alipay')->payVip($purchase);
	    return response()->json(['result' => $result, 'purchase_id' => $purchase->id]);
    }

    public function payVipPurchaseByWxpay(VipPurchase $purchase)
    {
    	$wxpay = cs("storm")->is_wxpay_open;
		if(empty($wxpay)){

			fail('微信支付暂不可用');
		}
	    $this->authorize('own', $purchase);
	    $result = cashier('wxpay')->payVip($purchase);
	    return response()->json(['result' => $result, 'purchase_id' => $purchase->id]);
    }

      public function payVipPurchaseByBalance(VipPurchase $purchase)
    {
	    $this->authorize('own', $purchase);
	    $result = cashier('balance')->payVip($purchase);
	    return response()->json(['result' => $result, 'purchase_id' => $purchase->id]);
    }

    public function payOrderByBalance(Order $order): OrderResource
	{
		$lock = Cache::lock('pay_order_by_banlance', 15);

		try {

			if (!$lock->get()) fail('高峰拥挤，请再试');

			$this->authorize('own', $order);
			$result = cashier('balance')->payOrder($order);

            return new OrderResource($result);

		} finally {
			$lock->release();
		}
	}
}
